package com.rcl.httpurlconnection.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.rcl.httpurlconnection.R;
import com.rcl.httpurlconnection.bean.CreateBean;
import com.rcl.httpurlconnection.bean.ResultCreateBean;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/*
 * class title:
 * Author by RenChunLin, Email 18957806320@163.com, Date on 2020/10/22.
 * PS: Not easy to write code, please indicate.
 */
public class PostRequestActivity extends AppCompatActivity {

    private TextView mName;
    private TextView mAge;
    private static Handler mHandler;
    private static final int RESULT_OK = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_request);

        initView();
        initHttp();
    }

    private void initHttp() {
        new Thread(new Runnable() {

            private InputStream mInputStream = null;
            private OutputStream mOutputStream = null;

            @Override
            public void run() {
                try {
                    URL url = new URL("http://192.168.0.123:3000/create");
                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                    urlConnection.setRequestMethod("POST");
                    urlConnection.setConnectTimeout(5000);
                    urlConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
                    urlConnection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9");
                    urlConnection.setRequestProperty("Accept", "application/json, text/plain, */*");

                    CreateBean createBean = new CreateBean("张三", "24");
                    Gson gson = new Gson();
                    String jsonStr = gson.toJson(createBean);
                    byte[] bytes = jsonStr.getBytes("UTF-8");
                    urlConnection.setRequestProperty("Content-Length", String.valueOf(bytes.length));
                    urlConnection.connect();

                    mOutputStream = urlConnection.getOutputStream();
                    mOutputStream.write(bytes);
                    mOutputStream.flush();
                    mOutputStream.close();

                    int responseCode = urlConnection.getResponseCode();
                    if (responseCode == HttpURLConnection.HTTP_OK) {
                        mInputStream = urlConnection.getInputStream();
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(mInputStream));
                        String line = bufferedReader.readLine();
                        bufferedReader.close();
                        Gson gson1 = new Gson();
                        ResultCreateBean resultCreateBean = gson1.fromJson(line, ResultCreateBean.class);
                        handlerResult(resultCreateBean);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void handlerResult(ResultCreateBean resultCreateBean) {
        Message message = mHandler.obtainMessage();
        message.what = RESULT_OK;
        message.obj = resultCreateBean;
        mHandler.sendMessage(message);
    }

    private void initView() {
        mName = findViewById(R.id.tv_name);
        mAge = findViewById(R.id.tv_age);

        mHandler = new Handler() {
            @Override
            public void handleMessage(@NonNull Message msg) {
                switch (msg.what) {
                    case RESULT_OK:
                        ResultCreateBean resultCreateBean = (ResultCreateBean) msg.obj;
                        mName.setText(resultCreateBean.getName());
                        mAge.setText(resultCreateBean.getAge());
                        break;
                }
            }
        };
    }
}
